package cn.wolfcode.trip.api.controller;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
 * 游记资源控制器
 */
@RequestMapping("/travels")
@RestController
public class TravelController {
    @Autowired
    private ITravelService travelService;

    @PostMapping
    public JsonResult save(Travel travel) {
        JsonResult jsonResult = new JsonResult();
        travelService.saveOrUpdate(travel);
        return jsonResult;
    }

    @PutMapping("{id}")
    public JsonResult update(Travel travel) {
        JsonResult jsonResult = new JsonResult();
        travelService.saveOrUpdate(travel);
        return jsonResult;
    }



    /**
     * 回显在评论中的封面和标题
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Travel selectTravelById(@PathVariable("id") Long id) {
        return travelService.selectTravelById(id);
    }

    @GetMapping("releasedTravels")
    public PageInfo selectReleasedTravels(TravelQueryObject qo) {
        //设置状态
        qo.setState(Travel.STATE_RELEASED);
        //设置公开
        qo.setIsPublic(true);
        //排序
        qo.setOrderBy("t.releaseTime desc");
        return travelService.queryReasonable(qo);
    }

    /**
     * 增加浏览量
     */
    @GetMapping("/updateBrowseNumById/{id}")
    public void updateBrowseNumById(@PathVariable Long id) {
        travelService.updateBrowseNumById(id);
    }




}
